package logic

import (
	"article_vote/config"
	"article_vote/models"
	"fmt"
)

type AuthorLogic struct {}

func (model *AuthorLogic) Create(name string) error {
	dbObj, _ := config.GetDbObj("default")
	redisObj, _ := config.GetRedis("default")

	author := &models.Author{}
	author.Name = name
	if err := dbObj.Create(author).Error; err != nil {
		return err
	}

	key := "author:" + fmt.Sprint(author.ID)
	authorInfo := make(map[string]interface{})
	authorInfo["id"] = author.ID
	authorInfo["name"] = author.Name
	authorInfo["created_at"] = author.CreatedAt.Unix()
	redisObj.HMSet(key, authorInfo)

	return nil
}
